<template>
    <div>
        <nav-com title="网体归属" :showView='showView' @back="backHandler()"></nav-com>
        <div class="margin_box">
            <van-form @submit="onSubmit">
                <van-row class="padd_box">
                    <van-col span="24">
                        <div class="login_box" style=" ">
                            <label>当前分享人：{{referralParentFttUser.cellphone}}({{referralParentFttUser.roleLevel | userRoleLevel}})<span style="color:#FF0000">{{parentUserText}}</span></label>
                            
                        </div>
                       
                    </van-col>
                </van-row>
                <van-row class="padd_box">
                    <van-col span="24">
                        <div class="login_box" style=" ">
                            <label>确认新归属：</label>
                            <input type="text" v-model='referralCode' @blur="ReferralCodeHandler(referralCode)"  placeholder="请输入新归属人分享码" />
                        </div>
                       
                    </van-col>
                </van-row>
                
                <van-row v-if="success" class="padd_box">
                    <van-col span="24">
                        <div class="login_box" style=" ">
                            <label>{{successData.cellphone}}({{successData.roleLevel | userRoleLevel}})</label>
                        </div>
                    </van-col>
                </van-row>
                
                <van-row class="padd_box" style="margin-top:10.66vw;">
                    <van-col span="24">
                        <van-button style="font-size:4.8vw; height:13.33vw;" color="#FF491F" type="info" native-type='submit' round  block>确认</van-button>
                    </van-col>
                </van-row>
                <div class="y_text">
                    仅支持修改一次，请谨慎操作
                </div>
            </van-form>
        </div>
    </div>
</template>
<script>
import {putFttusersParents,getFttuserUserId,getFttusersReferralCode,getReferralsummary }  from '@/api/api'
import navCom from '@/component/nav'
export default {
    name:'ascription',
    components:{
        navCom
    },
    data() {
        return {
            showView:true,
            uid: weixin.getUserId(),
            parentUserId:'',//上级
            referralCode:'',//归属码
            parentUserText:'',//显示无效
            
            success:false,
            successData:{},
            referralParentFttUser:'',

        }
    },
    mounted(){
        this.init()
    },
    methods:{
        //返回
        backHandler(){
            this.$router.go(-1)
        },
        //初始化
        init(){
            //当前用户
            getFttuserUserId({userId:this.uid}).then(res=>{
                if(res.data.code == 200){
                    if(res.data.data.parentUserId === 0){
                        this.parentUserText = '无效'
                        //this.referralParentUserId = res.data.data.referralParentUserId
                    }
                }else {
                    this.$dialog.alert({
                        message:res.data.msg
                    })
                }
            })
            getReferralsummary({userId:this.uid}).then(res=>{
                if(res.data.code == 200){
                    this.referralParentFttUser = res.data.data.referralParentFttUser
                }else {
                    this.$dialog.alert({
                        message:res.data.msg
                    })
                }
            })
            
        },
        ReferralCodeHandler(val){
            if(val == '' || val.trim().length==0){
                this.$dialog.alert({
                    message:'请输入新归属分享码'
                })
                return false
            }
            
            getFttusersReferralCode({referralCode:this.referralCode}).then(res=>{
                if(res.data.code == 200){
                    this.success = true
                    this.successData = res.data.data


                }else {
                    this.success = false
                    this.$dialog.alert({
                        message:res.data.msg
                    })
                }
            })
        },
        //保存
        onSubmit(){
            let _this = this
            if(this.referralCode ==''){
                this.$dialog.alert({
                    message:'新归属分享码不能为空'
                })
                return false
            }
            
            if(this.successData.userId == undefined){
                this.$dialog.alert({
                    message:'新归属分享码无效,请重新输入'
                })
                return false
            }
            if(this.successData.userId == this.uid){
                this.$dialog.alert({
                    message:'新归属分享码不能是自己'
                })
                return false
            }
            putFttusersParents({userId:this.uid,parentUserId:this.successData.userId}).then(res=>{
                if(res.data.code == 200){
                    this.$dialog.alert({
                        message:res.data.msg
                    }).then(res=>{
                        _this.$router.push('/my')
                    })
                }else{
                    this.$dialog.alert({
                        message:res.data.msg
                    })
                }
            })
        },
    }
}
</script>
<style scoped>
p {padding:0; margin:0;}
.margin_box {margin-left:4vw; margin-right:4vw; margin-top:11.2vw;}
.padd_box {min-height:10.66vw;  margin-top:3.4vw;  border-radius: 1.333vw;}
.ashBg {background: #fff; border-radius: 25px; border:1px solid #e3e3e3; width:100%;}
.login_box {border:1px solid #e3e3e3; font-size:4vw; border-radius: 6.66vw; height:13.3vw; line-height:13.3vw;}
.login_box label {padding-left:5.3vw; }
.login_box input { height:11vw; line-height: 11vw; border:none;}
.y_text {margin-top:4vw; font-size:4vw; width:100%; color:#999; text-align: center;}
</style>